package top.ruanlitao.shortlink.project.common.factory;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.ruanlitao.shortlink.project.common.handler.GlobalUncaughtExceptionHandler;

import java.util.concurrent.ThreadFactory;

/**
 * 自定义线程工厂
 * 装饰器模式复用 Spring 的线程工厂
 */
@Slf4j
@RequiredArgsConstructor
public class ShortLinkThreadFactory implements ThreadFactory {

    private final ThreadFactory threadFactory;

    @Override
    public Thread newThread(Runnable r) {
        // 创建线程
        Thread thread = threadFactory.newThread(r);
        // 设置自定义异常捕获处理器
        thread.setUncaughtExceptionHandler(GlobalUncaughtExceptionHandler.getInstance());
        // 返回创建的线程
        return thread;
    }
}